#! /usr/bin/perl -w
#
# C. Junghans
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#version 0.42 added --keep option
#version 0.43 added --pic option
#version 0.44 added --pdf option
#version 0.45 added -gv option
#version 0.46 change style to test as standard
#version 0.47 change --pic output to .ps
#version 0.48 added -w option
#version 0.49 better short opts
#version 0.50 added -watch to gv
#version 0.51 removed Base name
#version 0.52 added --rotate
#version 0.53, 18.04.08 -- added --tight and change default behavious
#version 0.54, 25.04.08 -- better version
#version 0.55, 13.10.08 -- added GPL header
#version 0.5.6, 19.05.11 -- removed --pdf option
#version 0.5.7, 08.05.13 -- fixed a bug in -o extension detection
#version 0.5.8, 19.08.13 -- added --output(=-o), fail if output is in pics
#version 0.5.9, 20.09.13 -- make -t, --view work again

use strict;

$_=$0;
s#^.*/##;
my $progname=$_;
my $usage="Usage: $progname [OPTIONS] tex code\nHelp with --help\n";
my $output="myfile";
my $ending="eps";
my @packages;
my $equ=undef;
my $keep=undef;
my $pic=undef;
my $pdf=undef;
my $tight=undef;
my $nrpic=0;
my $viewer=undef;
my $textwidth=1.0;
my $angle=0;

while ((defined ($ARGV[0])) and ($ARGV[0] =~ /^\-/))
{
        if (($ARGV[0] !~ /^--/) and (length($ARGV[0])>2)){
           $_=shift(@ARGV);
           if ( $_ =~ /^-[opw]/ ) {
              unshift(@ARGV,substr($_,0,2),substr($_,2));
           }
           else{
              unshift(@ARGV,substr($_,0,2),"-".substr($_,2));
           }
        }
	if (($ARGV[0] eq "-h") or ($ARGV[0] eq "--help"))
	{
		print <<END;
$usage
Make a eps or ps file out of a tex code

Options:
-v, --version         Prints version
-h, --help            Show this help message
-p NAME               Add package
-o, --output FILE     Give the output filename
                      default: $output.$ending
--equ                 Run in equation mode
--keep                Do not delete the tex file
--pic                 Run in Picture mode
-t, --tight           tight bounding (uses bbbox_add.pl)
-w, --width NUMBER    Change textwidth of the pictures
                      (implies --pic)
                      default: $textwidth (*\\textwidth)
-r, --rotate          rotate pics by 90 degree
--view VIEWER         Run VIEWER at the end

Examples: $progname --equ -o test.eps \'\\omega\'
          $progname --equ  -o test \\\\omega
          $progname --equ -p color '\\textcolor{blue}{\\theta}\'
          $progname hallo
          $progname --pic *.eps

Report bugs and comments at https://code.google.com/p/gnuplotutils/issues/list
                         or junghans\@votca.org
END
		exit;
	}
	elsif (($ARGV[0] eq "-o") or ($ARGV[0] eq "--width")){
		shift(@ARGV);
		$output=shift(@ARGV);
	}
	elsif ($ARGV[0] eq "-p")
	{
		shift(@ARGV);
		push(@packages,shift(@ARGV));
	}
	elsif ($ARGV[0] eq "--equ")
	{
		shift(@ARGV);
		$equ='yes';
	}
	elsif ($ARGV[0] eq "--keep")
	{
		shift(@ARGV);
		$keep='yes';
	}
	elsif ($ARGV[0] eq "--pic")
	{
		shift(@ARGV);
		$pic='yes';
	}
	elsif (($ARGV[0] eq "-w") or ($ARGV[0] eq "--width"))
	{
		shift(@ARGV);
		$pic='yes';
		$textwidth=shift(@ARGV);
	}
	elsif (($ARGV[0] eq "-r") or ($ARGV[0] eq "--rotate"))
	{
		shift(@ARGV);
		$pic='yes';
		$angle=90;
	}
	elsif (($ARGV[0] eq "--tight")  or ($ARGV[0] eq "-t"))
	{
		shift(@ARGV);
		$tight='yes';
	}
	elsif ($ARGV[0] eq "--view")
	{
		shift(@ARGV);
		$viewer=shift(@ARGV);
	}
	elsif (($ARGV[0] eq "-v") or ($ARGV[0] eq "--version"))
	{
		my $version=`perl -ne 'print "\$1\n" if /^#(version .*) --/' $0 | perl -ne 'print if eof'`;
		chomp($version);
		print "$progname, $version  by C. Junghans\n";
		exit;
	}
	elsif ($ARGV[0] eq "--hg")
	{
		my $message=`perl -ne 'print "\$1\n" if /^#version .*? -- (.*)\$/' $0 | perl -ne 'print if eof'`;
		chomp($message);
		print "$progname: $message\n";
		exit;
	}
	else
	{
		print "Unknow option '".$ARGV[0]."' ignored !\n";
		shift(@ARGV);
	}
}

die "${usage}Error: no tex code given\n" unless $#ARGV>-1;

die "What do you want equ or pic mode?\nCheat the text mode with \$something\$ !\n" if ($pic and $equ);

if ( $output =~ /^.*\.(.*?)$/){
  $ending=$1;
  die "Can not handle this ending ($ending)!\n" unless (($ending eq "eps")or($ending eq "ps")or($ending eq "pdf"));
}else{
  print "No ending given use .eps\n";
  $ending="eps";
  $output="$output.eps";
}
my $ending2="$ending";
$ending2="eps" if ( $ending eq "pdf");

my $trunc;
if ( $output =~ /^(.*)\.$ending$/){
  $trunc=$1;
}else{
  die "Could not get trunc from $output\n";
}

open(FILE,"> $trunc.tex") or
	die "Error at opening file\n";

print FILE '\documentclass[10pt,a4paper,onecolumn]{article}'."\n";
print FILE '\usepackage{amsmath}'."\n" if ($equ);
print FILE '\usepackage{graphicx}'."\n" if ($pic);
foreach (@packages){
	print FILE '\usepackage{'.$_."}\n";
}
print FILE '\begin{document}'."\n";
print FILE '\pagestyle{empty}'."\n";
print FILE '\noindent'."\n";
print FILE '\['."\n" if($equ);
if ($pic){
  foreach (@ARGV){
    if ( $_ !~ /\.(eps|ps)$/ ){
      print "Given picture ($_) should end with eps or ps -> ignored !\n";
      next;
    }
    if ( ! -r $_ ){
      print "Given picture is not readable -> ignored!\n";
      next;
    }
    if ( $_ =~ /(\.\/)?$trunc\.$ending2$/) {
      print "Given picture ($_) is equal to output file or one of his itermediates -> ignored!\n";
      next;
    }
    print FILE '\includegraphics[angle='.$angle.',width='.$textwidth.'\textwidth]{'.$_."}\n";
    $nrpic++;
  }
}else{
  foreach (@ARGV){
    print FILE $_." ";
  }
  print FILE "\n";
}
print FILE '\]'."\n" if ($equ);
print FILE '\end{document}'."\n";
close(FILE);
if ( ($pic) and ($nrpic==0))
{
	unlink("$trunc.tex") unless $keep;
	die "Picture mode and no valid picture found -> exit!\n";
}
$_=system("latex -halt-on-error $trunc.tex");
unlink("$trunc.tex") unless $keep;
unlink("$trunc.aux");
unlink("$trunc.log");
die "Error at running latex\n" if $_;
#no tight box for multiple pics
if(($tight)and($nrpic<2)){
   die "Error at running dvips\n" if system("dvips -E $trunc.dvi -o $trunc.$ending2");
} else {
  die "Error at running dvips\n" if system("dvips $trunc.dvi -o $trunc.$ending2");
}
unlink("$trunc.dvi");
if ($ending eq "pdf"){
	die "Error at running ps2pdf\n" if system("ps2pdf $trunc.$ending2");
	unlink("$trunc.$ending2");
}elsif(($tight)and ($nrpic<2)){
  #no tight box for multiple pics
  die "Error at runing bbbox_add.pl\n" if system("bbox_add.pl $trunc.$ending2");
}

if ($viewer){
  die "Error at runing $viewer\n" if system("$viewer $output &");
}
